package com.tinem.platform.module.starter.web.context;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className PageReq
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-14 21:07
 */
@Data
public class PageReq {

    public static void getMybatisPageRequest(HttpServletRequest request){
        getMybatisPageRequest(request,1000);
    }
    public static void getMybatisPageRequest(HttpServletRequest request,int maxPageSize){
        String pageCurrentString = request.getParameter("pageCurrent");
        if(StrUtil.isEmpty(pageCurrentString)){
            pageCurrentString = "0";
        }
        if(!NumberUtil.isInteger(pageCurrentString)){
            pageCurrentString = "0";
        }
        Integer pageCurrent = NumberUtil.parseInt(pageCurrentString);
        if(pageCurrent < 0){
            pageCurrent = 0;
        }

        String pageSizeString = request.getParameter("pageSize");
        if(StrUtil.isEmpty(pageSizeString)){
            pageSizeString = "5";
        }
        if(!NumberUtil.isInteger(pageSizeString)){
            pageSizeString = "5";
        }
        Integer pageSize = NumberUtil.parseInt(pageSizeString);
        if(pageSize <= 0){
            pageSize = 1;
        }
        if(pageSize > maxPageSize ){
            pageSize = maxPageSize;
        }

        String orderBy = request.getParameter("orderBy");
        PageHelper.startPage(pageCurrent+1, pageSize,orderBy);
    }

    public static PageRequest getPageRequest(HttpServletRequest request){
        return getPageRequest(request,1000);
    }
    public static PageRequest getPageRequest(HttpServletRequest request,int maxPageSize){
        String pageCurrentString = request.getParameter("pageCurrent");
        if(StrUtil.isEmpty(pageCurrentString)){
            pageCurrentString = "0";
        }
        if(!NumberUtil.isInteger(pageCurrentString)){
            pageCurrentString = "0";
        }
        Integer pageCurrent = NumberUtil.parseInt(pageCurrentString);
        if(pageCurrent < 0){
            pageCurrent = 0;
        }

        String pageSizeString = request.getParameter("pageSize");
        if(StrUtil.isEmpty(pageSizeString)){
            pageSizeString = "5";
        }
        if(!NumberUtil.isInteger(pageSizeString)){
            pageSizeString = "5";
        }
        Integer pageSize = NumberUtil.parseInt(pageSizeString);
        if(pageSize <= 0){
            pageSize = 1;
        }
        if(pageSize > maxPageSize ){
            pageSize = maxPageSize;
        }

        String orderBy = request.getParameter("orderBy");
        List<Sort.Order> orders = new ArrayList<>();
        if(StrUtil.isNotEmpty(orderBy)){
            for(String ob : orderBy.split(",")){
                String[] propertion = ob.trim().split(" ");
                String name = propertion[0];
                String direction = propertion.length > 1 ? propertion[propertion.length-1]: Sort.DEFAULT_DIRECTION.name();
                switch (Sort.Direction.valueOf(direction.toUpperCase())){
                    case ASC:
                        orders.add(Sort.Order.asc(name));
                        break;
                    case DESC:
                        orders.add(Sort.Order.desc(name));
                        break;
                    default:
                        continue;
                }
            }
        }

        return PageRequest.of(pageCurrent,pageSize,StrUtil.isEmpty(orderBy)?Sort.unsorted():Sort.by(orders));
    }
}
